package aom.ju.ss.view.dialog

import android.app.Activity
import aom.ju.ss.R
import aom.ju.ss.databinding.SetPostAgeBinding
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.mode.UserInfo
import aom.ju.ss.utils.LogUtils
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.show
import aom.ju.ss.view.user.shop.management.freightTemplate.PostageTemplate
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName

/**
 * 设置 邮费
 */
class SetPostage(
    activity: Activity?,
    var goodId: String?,
    var mode: PostageTemplate,
    var call: SetPostageCall
) :
    BaseDialog(activity) {

    init {
        val binding = vBinding as SetPostAgeBinding

        binding.gb.setOnClickListener {
            dis()
        }

        binding.cg.textView41.text = "常规地区邮费："
        binding.py.textView41.text = "偏远地区邮费："
        // 1特惠邮费 2常规邮费 3偏远地区邮费 4不配送区域

        if (goodId != null) {
            OKHttpUtils(activity)
                .SetApiUrl(LP_API.get_freight)
                .SetKey("api_version", "token", "client_id", "platform", "goods_id")
                .SetValue("v2", UserInfo.token, "1", "android", goodId)
                .POST(object : MyHttpCallBack {
                    override fun ok(jsonString: String?, httpTY: String?) {
                        val mode = Gson().fromJson(jsonString, getGoodsExpressFeeMode::class.java)
                        mode.data.data?.let { ac ->
                            for (a in ac) {
                                when (a.shippingId) {
                                    1 -> {
                                        binding.th.num.setText(a.price)
                                    }

                                    2 -> {
                                        binding.cg.num.setText(a.price)
                                    }
                                    3 -> {
                                        binding.py.num.setText(a.price)
                                    }
                                }
                            }

                        }
//                        if (binding.th.num.text.toString().isEmpty()) {
//                            "请输入特惠地区邮费".show()
//                            return@setOnClickListener
//                        }
//                        if (binding.cg.num.text.toString().isEmpty()) {
//                            "请输入常规地区邮费".show()
//                            return@setOnClickListener
//                        }
//
//                        if (binding.py.num.text.toString().isEmpty()) {
//                            "请输入偏远地区邮费".show()
//                            return@setOnClickListener
//                        }
                    }

                    override fun error(e: String?, jsonString: String?, httpTY: String?) {
                        e?.show

                    }



                })

        }

        LogUtils.e("_------" + Gson().toJson(mode.data))
        mode.data?.let {
            for (a in it) {
                var b = ""
                for ((i, c) in a.province.withIndex()) {
                    b += c
                    if (i != a.province.size - 1) {
                        b += ","
                    }
                }
                LogUtils.e(b)
                when (a.type) {
                    1 -> {
                        binding.th.tv.text = "区域:$b"
                    }
                    2 -> {
                        binding.cg.tv.text = "区域:$b"
                    }
                    3 -> {
                        binding.py.tv.text = "区域:$b"
                    }
                }
            }
        }


        binding.seve.setOnClickListener {
            if (binding.th.num.text.toString().isEmpty()) {
                "请输入特惠地区邮费".show()
                return@setOnClickListener
            }
            if (binding.cg.num.text.toString().isEmpty()) {
                "请输入常规地区邮费".show()
                return@setOnClickListener
            }

            if (binding.py.num.text.toString().isEmpty()) {
                "请输入偏远地区邮费".show()
                return@setOnClickListener
            }
            call.ok(
                binding.th.num.text.toString(),
                binding.cg.num.text.toString(),
                binding.py.num.text.toString()
            )
            dis()
        }
        binding.gb.setOnClickListener { dis() }
    }


    override fun init() {

    }


    override fun getLay(): Int {
        return R.layout.set_post_age
    }
}

/**
 *  特惠 常规 偏远
 */
interface SetPostageCall {
    fun ok(th: String, cg: String, py: String)
}

data class getGoodsExpressFeeMode(
    @SerializedName("data")
    var `data`: getGoodsExpressFeeModeData,
    @SerializedName("mark")
    var mark: String,
    @SerializedName("mess")
    var mess: String,
    @SerializedName("other")
    var other: String,
    @SerializedName("status")
    var status: Int
)

data class getGoodsExpressFeeModeData(
    @SerializedName("data")
    var `data`: List<getGoodsExpressFeeModeDataData>?
)

data class getGoodsExpressFeeModeDataData(
    @SerializedName("price")
    var price: String,
    @SerializedName("province")
    var province: String,
    @SerializedName("shipping_id")
    var shippingId: Int,
    @SerializedName("shipping_name")
    var shippingName: String,
    @SerializedName("type")
    var type: Int
)